import "./tab.css";
import { getData } from "api/getData";

const baseURL = "https://www.imooc.com/api/mall-wepApp/destination/content";
// const baseURL = "https://www.imooc.com/api/mall-wepApp/index/nav";

class Tabs {
  constructor(el) {
    this.tabs = el.getElementsByClassName("tab-item");
  }

  setToActive(index) {
    for (let item of this.tabs) {
      item.classList.remove("tab-item-active");
    }

    this.tabs[index].classList.add("tab-item-active");
  }

  to(index) {
    if (this.dataPromise && this.dataPromise.xhr) {
      this.dataPromise.xhr.abort();
    }

    this.dataPromise = getData(`${baseURL}/${this.tabs[index].dataset.id}`);

    this.setToActive(index);
    return this.dataPromise;
  }
}
//${this.tabs[index].dataset.id}
export { Tabs };
